package utils

import (
	"fmt"

	"gopkg.in/ini.v1"
)

var (
	AppMode  string
	HttpPort string

	DbType string

	SqliteFile string

	MysqlHost     string
	MysqlPort     string
	MysqlUser     string
	MysqlPassword string
	MysqlDbName   string

	PgHost     string
	PgPort     string
	PgUser     string
	PgPassword string
	PgDbName   string
	PgSslMode  string
	PgTimeZone string
)

func init() {
	file, err := ini.Load("conf/config.ini")
	if err != nil {
		fmt.Println("读配置文件错误: ", err)
	}

	LoadServer(file)
	LoadDatabase(file)
}

func LoadServer(file *ini.File) {
	AppMode = file.Section("server").Key("AppMode").MustString("debug")
	HttpPort = file.Section("server").Key("HttpPort").MustString(":3000")
}

func LoadDatabase(file *ini.File) {
	DbType = file.Section("database").Key("Type").MustString("mysql")

	SqliteFile = file.Section("sqlite").Key("File").MustString("data/wlblog.db")

	MysqlHost = file.Section("mysql").Key("Host").MustString("127.0.0.1")
	MysqlPort = file.Section("mysql").Key("Port").MustString("3306")
	MysqlUser = file.Section("mysql").Key("User").MustString("wlblog")
	MysqlPassword = file.Section("mysql").Key("Password").MustString("Wlblog!23")
	MysqlDbName = file.Section("mysql").Key("DbName").MustString("wlblog")

	PgHost = file.Section("postgres").Key("Host").MustString("127.0.0.1")
	PgPort = file.Section("postgres").Key("Port").MustString("3306")
	PgUser = file.Section("postgres").Key("User").MustString("wlblog")
	PgPassword = file.Section("postgres").Key("Password").MustString("Wlblog!23")
	PgDbName = file.Section("postgres").Key("DbName").MustString("wlblog")
	PgSslMode = file.Section("postgres").Key("SslMode").MustString("disable")
	PgTimeZone = file.Section("postgres").Key("TimeZone").MustString("Asia/Shanghai")
}
